import java.util.ArrayList;
import java.util.List;

public class Solution386 {
    public List<Integer> lexicalOrder(int n) {
        StringBuilder stringBuilder=new StringBuilder();
        List<Integer> list=new ArrayList<>();
        dfs(stringBuilder,0,list,n);
        return list;
    }

    private void dfs(StringBuilder stringBuilder, int k, List<Integer> list, int n) {
        int start=k==0?1:0;
        for (int i=start;i<=9;i++){
            stringBuilder.append(i);
            if (Integer.parseInt(stringBuilder.toString())>n){
                stringBuilder.deleteCharAt(stringBuilder.length()-1);
                break;
            }
            list.add(Integer.parseInt(stringBuilder.toString()));
            dfs(stringBuilder,k+1,list,n);
            stringBuilder.deleteCharAt(stringBuilder.length()-1);
        }
    }

    public static void main(String[] args) {
        System.out.println(new Solution386().lexicalOrder(13));
    }
}
